$(function () {


    //初始化员工列表
    $("#gift_datagrid").datagrid({
        fitColumns:true,
        fit:true,
        url:"/personnel/list",
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        striped:true,
        toolbar: '#tb',
        columns:[[
            // {field:"ck",checkbox:true},  //显示多选框样式
            {field:"id", title:"编号", hidden:true},
            {field:"jobNumber", title:"工号", width:100},
            {field:"name", title:"姓名", width:100},
            {field:"dept", title:"部门", width:100},
            {field:"professionalTitle", title:"职称", width:100},
            {field:"hiredate", title:"入职时间", width:100},
        ]],


    });
    //end


    //初始礼员工考勤表 gift_datagrid
    $("#check_datagrid").datagrid({
        title:"员工考勤表",
        url:'/check/list',
        fitColumns:true,
        fit:true,
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        striped:true,
        columns:[[
            {field:'id',title:'编号',width:100},
            {field:'empId',title:'员工编号',width:100},
            {field:'empName',title:'员工姓名',width:100,align:'right'},
            {field:'beginTime',title:'上班打卡',width:100,align:'right'},
            {field:'endTime',title:'下班打卡',width:100,align:'right'},
            {field:'date',title:'日期',width:100,align:'right'},
            {field:'state',title:'状态',width:100,align:'right'},
            {field:'remark',title:'备注',width:100,align:'right'},
            {field:'checkCost',title:'其扣',width:100,align:'right'}
        ]]
    });

    //初始化工资表 gift_datagrid
    $("#salary_datagrid").datagrid({
        title: "工资列表",
        url: '/salary/list',
        fitColumns: true,
        fit: true,
        //closed:true,
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        striped: true,
        columns: [[
            {field: 'id', title: '编号', width: 100},
            {field: 'name', title: '姓名', width: 100},
            {field: 'position', title: '职位', width: 100, align: 'right'},
            {field: 'basePay', title: '基本工资', width: 100, align: 'right'},
            {field: 'performancePay', title: '绩效工资', width: 100, align: 'right'},
            {field: 'technologyPay', title: '技术补贴', width: 100, align: 'right'},
            {field: 'phonePay', title: '通讯费', width: 100, align: 'right'},
            {field: 'awardPay', title: '奖励', width: 100, align: 'right'},
            {field: 'overworkPay', title: '加班', width: 100, align: 'right'},
            {field: 'stayworkPay', title: '值班', width: 100, align: 'right'},
            {field: 'teachCost', title: '讲课费', width: 100, align: 'right'},
            {field: 'yearPay', title: '年终奖', width: 100, align: 'right'},
            {field: 'olderPay', title: '工资工龄', width: 100, align: 'right'},
            {field: 'complement', title: '补足', width: 100, align: 'right'},
            {field: 'cost', title: '扣款', width: 100, align: 'right'},
            {field: 'workdateCost', title: '考勤扣款', width: 100, align: 'right'},
            {field: 'salary', title: '应发工资', width: 100, align: 'right'},
            {field: 'socialCost', title: '社保', width: 100, align: 'right'},
            {field: 'funds', title: '公积金', width: 100, align: 'right'},
            {field: 'taxCost', title: '个所税', width: 100, align: 'right'},
            {field: 'trueSalary', title: '实发工资', width: 100, align: 'right'},
            {field: 'remark', title: '备注', width: 100, align: 'right'}
        ]],

    });
    //end

    //初始化保存或编辑对话框
    $("#gift_dialog").dialog({
        width: 300,
        height: 400,
        closed: true,
        buttons:"#bb"
    });

    //响应集合函数对象
    var cmdObj = {
        //顶部工具栏的刷新
        reload: function () {
            gift_datagrid.datagrid("load");
        },
        //顶部工具栏的考勤考核
        add: function () {
            //添加与编辑时将数据清空
            //check_datagrid.datagrid("clear");
            //判断是否选中数据
            var row = $("#gift_datagrid").datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要考核的员工');
            } else {
                //添加与编辑时将对话框中数据清空
                $('#gift_form').form("clear");
                //数据回显: 同名原则
                $("#gift_form").form("load", row);
                var rowCk = $("#check_datagrid").datagrid("getRows")[0];
                var cost =rowCk.checkCost;
                $("#checkCost").textbox("setText",cost);
                $("#gift_dialog").dialog("open");
                alert("发送");
                $("#check_datagrid").datagrid("load",{keyword:row.id});
                $("#salary_datagrid").datagrid("load",{keyword:row.name});
                $("#gift_dialog").dialog("setTitle", "考勤考核");

            }
        },

        showCheck:function () {
            var row = $("#gift_datagrid").datagrid("getSelected");
            $("#gift_dialog").dialog("close");
            $('#win').window({
                width:800,
                height:400,
                modal:true,
                onClose:function () {
                    $("#gift_dialog").dialog("open");
                }
            });


        },

        showSalary:function () {
            $("#gift_dialog").dialog("close");
            $('#win1').window({
                width:1200,
                height:400,
                modal:true,
                onClose:function () {
                    $("#gift_dialog").dialog("open");
                }
            });
            $("#salary_datagrid").datagrid("open");

        },
        save:function () {
            var row = $("#gift_datagrid").datagrid("getSelected");
            var rowCk = $("#check_datagrid").datagrid("getRows")[0];
            var cost =rowCk.checkCost;
            $.post("/salary/save",{name:row.name,position:row.professionalTitle,cost:cost},function (data) {
                if(data.success){
                    $.messager.alert('温馨提示','已生成'+row.name+'的工资单');
                }else{
                    $.messager.alert('温馨提示','操作失败');
                }
            });
        },
        cancel:function () {
            $("#gift_dialog").dialog("close");
        },

        //高级查询
        query:function () {
            //获取高级查询输入框的文本
            var kw = $("#keyword").textbox("getValue")
            if(!kw){
                $.messager.alert('温馨提示','请输入关键字');
                return;
            }
            gift_datagrid.datagrid("load",{
                keyword:kw
            })
        }
        //end

    };
    //end


    //调用
    $("a[data-cmd]").click(function(){
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    })
    //end
})